package com.example.socketdemo3;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author zhanghui
 * @date 2021/12/6 10:08
 */
public class Client {
    public static void main(String[] args) throws IOException {
        String serverIp = "127.0.0.1";
        int serverPort = 9999;

        System.out.println("客户端启动");
        // 连接服务器
        Socket socket = new Socket(serverIp, serverPort);
        System.out.println("已连接服务器");
        // 获取输出流, 向服务器发送数据
        OutputStream outputStream = socket.getOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.print("请输入: ");
            String msg = scanner.nextLine();
            printStream.println(msg);
            printStream.flush();
        }
    }
}
